﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Security.Cryptography;
using System.Web.Script.Serialization;
using System.IO;
using System.ComponentModel;
namespace EasyLib.Extensions
{
    /// <summary>
    /// 对 System.String 的扩展
    /// </summary>
    /// 
    public static class StringExtension
    {
        #region 私有成员

        /// <summary>
        /// 需要转义的字符正则
        /// </summary>
        static readonly Regex _transferredRule = new Regex(@"('|""|\\)", RegexOptions.Compiled);

        /// <summary>
        /// 空白字符正则
        /// </summary>
        static readonly Regex _spaceRule = new Regex(@"\s", RegexOptions.Compiled);

        /// <summary>
        /// HTML标签正则
        /// </summary>
        static readonly Regex _htmlTagRule = new Regex(@"<[^>]*>", RegexOptions.Compiled);

        /// <summary>
        /// 空字节数组
        /// </summary>
        static readonly Byte[] _emptyByteArray = new Byte[0];

        /// <summary>
        /// 默认的64位加密初始化向量
        /// </summary>
        static readonly Byte[] _defaultRgbIV64 = { 0x12, 0x65, 0xa3, 0xcd, 0x90, 0x4f, 0x53, 0xfc };

        /// <summary>
        /// 默认的128位加密初始化向量
        /// </summary>
        static readonly Byte[] _defaultRgbIV128 = { 0x12, 0x65, 0xa3, 0xcd, 0x90, 0x4f, 0x53, 0xfc, 0x1f, 0xa5, 0xa1, 0xcd, 0x10, 0xef, 0x5b, 0xac };

        #endregion

        #region 基本功能
        /// <summary>
        /// 指示 System.string 对象是 null 或者 System.String.Empty。
        /// </summary>
        /// <param name="str">一个 System.String 引用</param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string str)
        {
            return String.IsNullOrEmpty(str);
        }

        /// <summary>
        /// 返回如果 System.string 对象是 null 则返回  System.String.Empty 否则返回他本身。
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string IfNullReturnEmpty(this string str)
        {
            if (str == null)
                return string.Empty;
            return str;
        }

        /// <summary>
        /// 返回 System.string 对象中从左边开始指定个数的字符。
        /// </summary>
        /// <param name="str">一个 System.String 引用</param>
        /// <param name="length">正整数，将返回的字符数</param>
        /// <returns></returns>
        /// <example>
        /// <code>
        ///  String str="abcdefg";
        ///  Console.WriteLine(str.Left(3));
        /// </code>
        /// //abc
        /// </example>
        public static string Left(this string str, int length)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (length < 0)
                throw new ArgumentOutOfRangeException("length");

            if (str.Length == 0 || str.Length <= length)
                return str;

            return str.Substring(0, length);
        }

        /// <summary>
        /// 返回 System.string 对象中从右边开始指定个数的字符。
        /// </summary>
        /// <param name="str">一个 System.String 引用</param>
        /// <param name="length">正整数，将返回的字符数</param>
        /// <returns></returns>
        /// <example>
        /// 	<code>
        ///  String str="abcdefg";
        ///  Console.WriteLine(str.Right(3));
        ///  </code>
        ///  //efg
        ///  </example>
        public static string Right(this string str, int length)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (length < 0)
                throw new ArgumentOutOfRangeException("length");

            if (str.Length == 0 || str.Length <= length)
                return str;

            var index = str.Length - length;
            return str.Substring(index);
        }

        /// <summary>
        /// 返回 System.String 对象从左边劈掉1个字符后的字符串
        /// </summary>
        /// <example>
        /// <code>
        /// String str="abcdef";
        /// Console.WriteLine(str.ChopLeft())
        /// </code>
        /// //bcdef
        /// </example>
        /// <param name="str">一个 System.String 引用</param>
        public static string ChopLeft(this string str)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (str.Length == 0)
                return str;

            return ChopLeft(str, 1);
        }

        /// <summary>
        /// 返回 System.String 对象从左边劈掉指定个字符后的字符串
        /// </summary>
        /// <example>
        /// <code>
        /// String str="abcdef";
        /// Console.WriteLine(str.ChopLeft(2))
        /// </code>
        /// //cdef
        /// </example>
        /// <param name="str">一个 System.String 引用</param>
        /// <param name="count">劈掉的字符个数</param>
        public static string ChopLeft(this string str, int count)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (count < 0)
                throw new ArgumentOutOfRangeException("length");

            if (str.Length == 0 || str.Length <= count)
                return string.Empty;

            return str.Substring(count);
        }

        /// <summary>
        /// 返回 System.String 对象从右边劈掉1个字符后的字符串
        /// </summary>
        /// <example>
        /// <code>
        /// String str="abcdef";
        /// Console.WriteLine(str.ChopRight())
        /// </code>
        /// //abcde
        /// </example>
        /// <param name="str">一个 System.String 引用</param>
        public static string ChopRight(this string str)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            return ChopRight(str, 1);
        }

        /// <summary>
        /// 返回 System.String 对象从右边劈掉指定个字符后的字符串
        /// </summary>
        /// <example>
        /// 
        /// 
        /// </example>
        /// <param name="str">一个 System.String 引用</param>
        /// <param name="count">劈掉的字符个数</param>
        public static string ChopRight(this string str, int count)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (count < 0)
                throw new ArgumentOutOfRangeException("length");

            if (str.Length == 0 || str.Length <= count)
                return String.Empty;

            return str.Substring(0, str.Length - count);
        }

        /// <summary>
        /// 返回 从 System.String 对象中删除了所有指定字符串之后的字符串
        /// </summary>
        /// <param name="str">一个 System.String 引用</param>
        /// <param name="deleteStr">将被删除的字符串</param>
        public static string Remove(this string str, string deleteStr)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (deleteStr == null ||
                    str.Length == 0 ||
                    deleteStr.Length == 0 ||
                    str.Length < deleteStr.Length)
                return str;

            StringBuilder sb = new StringBuilder(str);
            return sb.Replace(deleteStr, string.Empty).ToString();

        }

        /// <summary>
        /// 返回 从 System.String 对象中删除了所有指定的一系列字符串之后的字符串
        /// </summary>
        /// <param name="str">一个 System.String 引用</param>
        /// <param name="deleteStrs">将被删除的字符串</param>
        public static string Remove(string str, params string[] deleteStrs)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (deleteStrs == null ||
                    str.Length == 0 ||
                    deleteStrs.Length == 0)
                return str;

            StringBuilder sb = new StringBuilder(str);
            foreach (var i in deleteStrs)
            {
                if (i == null ||
                    i.Length == 0 ||
                    i.Length > str.Length)
                    continue;

                sb.Replace(i, string.Empty);
            }

            return sb.ToString();
        }
        /// <summary>
        /// 返回 System.String 对象重复拷贝指定次数之后的字符串
        /// </summary>
        /// <param name="str">一个 System.String 引用</param>
        /// <param name="count">拷贝的次数</param>
        /// <returns></returns>
        public static string Replicate(this string str, int count)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (count < 0)
                throw new ArgumentOutOfRangeException("count");

            if (count < 1)
                return str;

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                sb.Append(str);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 返回 从 System.String 对象中删除了一系列空白字符 如：空白字符，包括空格、制表符、换页符等等  之后的字符串
        /// </summary>
        /// <param name="str">一个 System.String 引用</param>
        /// <returns></returns>
        public static string RemoveSpaceString(this string str)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            return _spaceRule.Replace(str, string.Empty);
        }

        /// <summary>
        /// 断言 System.String 对象中字符个数是否在指定的范围之内
        /// </summary>
        /// <param name="str">一个 System.String 引用</param>
        /// <param name="minLength">字符个数最少不能小于的个数</param>
        /// <param name="maxLength">字符个数最多不能大于的个数</param>
        /// <returns></returns>
        public static bool LengthRange(this string str, int minLength, int maxLength)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (minLength < 0)
                throw new ArgumentOutOfRangeException("maxLength");

            if (maxLength < minLength || maxLength < 0)
                throw new ArgumentOutOfRangeException("maxLength");

            return str.Length >= minLength && str.Length <= maxLength;
        }

        /// <summary>
        /// 将指定的 JSON 字符串转换为 T 类型的对象
        /// </summary>
        /// <typeparam name="T">所生成对象的类型</typeparam>
        /// <param name="str">要进行反序列化的 JSON 字符串</param>
        /// <returns></returns>
        public static T JSONDeserialize<T>(this string str)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Deserialize<T>(str);
        }

        /// <summary>
        /// 将指定的 JSON 字符串转换为对象图
        /// </summary>
        /// <param name="str">要进行反序列化的 JSON 字符串</param>
        /// <returns></returns>
        public static object JSONDeserialize(this string str)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            var jss = new JavaScriptSerializer();
            return jss.DeserializeObject(str);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="separator"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int[] Split(this string str, string separator, int defaultValue)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (separator == null)
                throw new ArgumentNullException("separator");

            if (separator.Length == 0)
                throw new ArgumentException("separator");

            string[] numbersStringArray = str.Split(new string[] { separator },
                                                        StringSplitOptions.RemoveEmptyEntries);
            int[] numbers = new int[numbersStringArray.Length];

            for (int i = 0; i < numbersStringArray.Length; i++)
            {
                string s = numbersStringArray[i];
                if (!Int32.TryParse(s, out numbers[i]))
                    numbers[i] = defaultValue;
            }

            return numbers;

        }

        public static string Choose()
        {
            throw new System.NotImplementedException();
        }

        public static long ToLongFromIPv4()
        {
            throw new System.NotImplementedException();
        }

        public static string Stuff()
        {
            throw new System.NotImplementedException();
        }

        public static T[] Split<T>()
        {
            throw new System.NotImplementedException();
        }


        #endregion

        #region 正则表达式

        public static bool IsMatch(string str, string pattern)
        {
            throw new System.NotImplementedException();
        }

        public static bool IsMatch(string str, string pattern, RegexOptions options)
        {
            throw new System.NotImplementedException();
        }

        public static Match Match(string str, string pattern)
        {
            throw new System.NotImplementedException();
        }

        public static Match Match(string str, string pattern, RegexOptions options)
        {
            throw new System.NotImplementedException();
        }

        public static MatchCollection Matchs(string str, string pattern)
        {
            throw new System.NotImplementedException();
        }

        public static MatchCollection Matchs(string str, string pattern, RegexOptions options)
        {
            throw new System.NotImplementedException();
        }

        public static void Matchs(string str, string pattern, Action<Match> action)
        {
            throw new System.NotImplementedException();
        }

        public static void Matchs(string str, string pattern, RegexOptions options, Action<Match> action)
        {
            throw new System.NotImplementedException();
        }

        public static string ReplaceX(string str, string pattern, string replacement)
        {
            throw new System.NotImplementedException();
        }

        public static string ReplaceX(string str, string pattern, string replacement, RegexOptions options)
        {
            throw new System.NotImplementedException();
        }

        public static string ReplaceX(string str, string pattern, MatchEvaluator evaluator)
        {
            throw new System.NotImplementedException();
        }

        public static string ReplaceX(string str, string pattern, RegexOptions options, MatchEvaluator evaluator)
        {
            throw new System.NotImplementedException();
        }

        #endregion

        #region WEB开发

        /// <summary>
        /// 返回 System.String 对象转义为Javascript脚本字符串常量之后的字符串
        /// </summary>
        /// <param name="str">一个 System.String 引用</param>
        /// <returns></returns>
        public static string ToJavascriptString(this string str)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (str.Length == 0)
                return str;

            str = _transferredRule.Replace(str, "\\$1");
            StringBuilder sb = new StringBuilder(str);
            sb.Replace("\r", "\\r");
            sb.Replace("\n", "\\n");
            return sb.ToString();

        }

        /// <summary>
        /// 返回 System.String 对象中将HTML标签删除之后的字符串
        /// </summary>
        /// <param name="str">HTML字符串</param>
        /// <returns></returns>
        public static string RemoveHtml(this string str)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (str.Length == 0)
                return str;

            return _htmlTagRule.Replace(str, string.Empty);
        }

        /// <summary>
        /// 将字符串转换为 HTML 编码的字符串。
        /// </summary>
        /// <param name="str">要编码的字符串</param>
        /// <returns></returns>
        public static string HtmlEncode(this string str)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (str.Length == 0)
                return str;

            return HttpUtility.HtmlEncode(str);
        }

        /// <summary>
        /// 将字符串转换为 HTML 编码的字符串并将输出作为 System.IO.TextWriter 输出流返回。
        /// </summary>
        /// <param name="str">要编码的字符串</param>
        /// <param name="output">System.IO.TextWriter 输出流</param>
        /// <returns></returns>
        public static void HtmlEncode(this string str, TextWriter output)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (output == null)
                throw new ArgumentNullException("output");

            HttpUtility.HtmlEncode(str, output);
        }

        /// <summary>
        /// 已经为 HTTP 传输进行过 HTML 编码的字符串转换为已解码的字符串。
        /// </summary>
        /// <param name="str">要解码的字符串</param>
        /// <returns></returns>
        public static string HtmlDecode(this string str)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (str.Length == 0)
                return str;

            return HttpUtility.HtmlDecode(str);
        }

        /// <summary>
        /// 将已经过 HTML 编码的字符串转换为已解码的字符串并将其发送给 System.IO.TextWriter 输出流。
        /// </summary>
        /// <param name="str">要解码的字符串</param>
        /// <param name="output">System.IO.TextWriter 输出流</param>
        /// <returns></returns>
        public static void HtmlDecode(this string str, TextWriter output)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (output == null)
                throw new ArgumentNullException("output");

            HttpUtility.HtmlDecode(str, output);
        }

        /// <summary>
        /// 对 URL 字符串进行编码。
        /// </summary>
        /// <param name="str">要编码的文本</param>
        /// <returns></returns>
        public static string UrlEncode(this string str)
        {

            if (str == null)
                throw new ArgumentNullException("str");

            if (str.Length == 0)
                return str;

            HttpContext httpContext = HttpContext.Current;
            if (httpContext != null)
            {
                return HttpUtility.UrlEncode(str, httpContext.Response.ContentEncoding);
            }
            else
            {
                return HttpUtility.UrlEncode(str);
            }
        }

        /// <summary>
        /// 使用指定的编码对象对 URL 字符串进行编码。
        /// </summary>
        /// <param name="str">要编码的文本</param>
        /// <param name="encoding">指定编码方案的 System.Text.Encoding 对象</param>
        /// <returns></returns>
        public static string UrlEncode(this string str, Encoding encoding)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (encoding == null)
                throw new ArgumentNullException("encoding");

            if (str.Length == 0)
                return str;

            return HttpUtility.UrlEncode(str, encoding);
        }

        /// <summary>
        /// 将已经为在 URL 中传输而编码的字符串转换为解码的字符串。
        /// </summary>
        /// <param name="str">要解码的字符串</param>
        /// <returns></returns>
        public static string UrlDecode(this string str)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            HttpContext httpContext = HttpContext.Current;
            if (httpContext != null)
            {
                return HttpUtility.UrlDecode(str, httpContext.Request.ContentEncoding);
            }
            else
            {
                return HttpUtility.UrlDecode(str);
            }
        }

        /// <summary>
        /// 使用指定的编码对象将 URL 编码的字符串转换为已解码的字符串。
        /// </summary>
        /// <param name="str">要解码的字符串</param>
        /// <param name="encoding">指定解码方法的 System.Text.Encoding</param>
        /// <returns></returns>
        public static string UrlDecode(this string str, Encoding encoding)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (encoding == null)
                throw new ArgumentNullException("encoding");

            if (str.Length == 0)
                return str;

            return HttpUtility.UrlDecode(str, encoding);
        }

        #endregion

        #region 加密解密

        #region MD5

        /// <summary>
        ///  返回 System.String 对象进行MD5加密后的32字符十六进制格式字符串 
        /// </summary>
        /// <param name="str">要加密的字符串</param>
        /// <returns></returns>
        public static string MD5Encrypt(this string str)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            MD5 md5 = MD5.Create();
            byte[] bytes = md5.ComputeHash(Encoding.Default.GetBytes(str));
            StringBuilder sb = new StringBuilder();

            foreach (var i in bytes)
            {
                sb.Append(i.ToString("x2"));
            }

            return sb.ToString();
        }

        #endregion

        #region DES

        /// <summary>
        /// 返回 System.String 对象使用指定向量来进行DES对称加密后的Base64字符串
        /// </summary>
        /// <param name="str">要加密的字符串</param>
        /// <param name="rgbIV">64位的初始化向量</param>
        /// <param name="key">进行加密的64位密钥</param>
        /// <returns></returns>
        public static string DESEncrypt(this string str, byte[] rgbIV, string key)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (rgbIV == null)
                throw new ArgumentNullException("rgbIV");

            if (key == null)
                throw new ArgumentNullException("key");

            byte[] keyBytes = Encoding.UTF8.GetBytes(key);
            if (keyBytes.Length != 8)
                throw new CryptographicException("密钥是64位");

            if (rgbIV.Length != 8)
                throw new CryptographicException("初始化向量是64位");

            byte[] inputBytes = Encoding.UTF8.GetBytes(str);
            DES des = DES.Create();
            ICryptoTransform cryp = des.CreateEncryptor(keyBytes, rgbIV);
            byte[] outputBytes = cryp.TransformFinalBlock(inputBytes, 0, inputBytes.Length);
            return Convert.ToBase64String(outputBytes);
        }

        /// <summary>
        ///  返回 System.String 对象使用固定向量来进行DES对称加密后的Base64字符串
        /// </summary>
        /// <param name="str">要加密的字符串</param>
        /// <param name="key">进行加密的64位密钥</param>
        /// <returns></returns>
        public static string DESEncrypt(this string str, string key)
        {
            return DESEncrypt(str, _defaultRgbIV64, key);
        }


        /// <summary>
        /// 对已进行DES加密的Base64字符串进行解密
        /// </summary>
        /// <param name="str">要解密的字符串</param>
        /// <param name="rgbIV">64位的初始化向量</param>
        /// <param name="key">进行解密的64位密钥</param>
        /// <returns></returns>
        public static string DESDecrypt(this string str, byte[] rgbIV, string key)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (rgbIV == null)
                throw new ArgumentNullException("rgbIV");

            if (key == null)
                throw new ArgumentNullException("key");

            byte[] keyBytes = Encoding.UTF8.GetBytes(key);
            if (keyBytes.Length != 8)
                throw new CryptographicException("密钥是64位");

            if (rgbIV.Length != 8)
                throw new CryptographicException("初始化向量是64位");

            byte[] inputBytes = Convert.FromBase64String(str);
            DES des = DES.Create();
            ICryptoTransform cryp = des.CreateDecryptor(keyBytes, rgbIV);
            byte[] outputBytes = cryp.TransformFinalBlock(inputBytes, 0, inputBytes.Length);
            return Encoding.UTF8.GetString(outputBytes);
        }

        /// <summary>
        ///  对已使用固定向量进行DES加密的Base64字符串进行解密
        /// </summary>
        /// <param name="str">要解密的字符串</param>
        /// <param name="key">进行解密的64位密钥</param>
        /// <returns></returns>
        public static string DESDecrypt(this string str, string key)
        {
            return DESDecrypt(str, _defaultRgbIV64, key);
        }

        #endregion

        #region AES

        /// <summary>
        /// 返回 System.String 对象使用指定向量来进行128位 AES对称加密后的Base64字符串
        /// </summary>
        /// <param name="str">要加密的字符串</param>
        /// <param name="rgbIV">128位的初始化向量</param>
        /// <param name="key">进行加密的128位密钥</param>
        /// <returns></returns>
        public static string AESEncrypt(this string str, byte[] rgbIV, string key)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (rgbIV == null)
                throw new ArgumentNullException("rgbIV");

            if (key == null)
                throw new ArgumentNullException("key");

            byte[] keyBytes = Encoding.UTF8.GetBytes(key);
            if (keyBytes.Length != 16)
                throw new CryptographicException("密钥应该是128位");

            if (rgbIV.Length != 16)
                throw new CryptographicException("初始化向量应该是128位");

            byte[] inputBytes = Encoding.UTF8.GetBytes(str);
            RijndaelManaged rm = new RijndaelManaged();
            rm.IV = rgbIV;
            rm.Key = keyBytes;

            ICryptoTransform cryp = rm.CreateEncryptor();
            byte[] outputBytes = cryp.TransformFinalBlock(inputBytes, 0, inputBytes.Length);
            return Convert.ToBase64String(outputBytes);
        }

        /// <summary>
        ///  返回 System.String 对象使用固定向量来进行128位 AES对称加密后的Base64字符串
        /// </summary>
        /// <param name="str">要加密的字符串</param>
        /// <param name="key">进行加密的128位密钥</param>
        /// <returns></returns>
        public static string AESEncrypt(this string str, string key)
        {
            return AESEncrypt(str, _defaultRgbIV128, key);
        }

        /// <summary>
        /// 对已进行128位 AES加密的Base64字符串进行解密
        /// </summary>
        /// <param name="str">要解密的字符串</param>
        /// <param name="rgbIV">128位的初始化向量</param>
        /// <param name="key">进行解密的128位密钥</param>
        /// <returns></returns>
        public static string AESDecrypt(this string str, byte[] rgbIV, string key)
        {
            if (str == null)
                throw new ArgumentNullException("str");

            if (rgbIV == null)
                throw new ArgumentNullException("rgbIV");

            if (key == null)
                throw new ArgumentNullException("key");

            byte[] keyBytes = Encoding.UTF8.GetBytes(key);
            if (keyBytes.Length != 16)
                throw new CryptographicException("密钥应该是128位");

            if (rgbIV.Length != 16)
                throw new CryptographicException("初始化向量应该是128位");

            byte[] inputBytes = Convert.FromBase64String(str);
            RijndaelManaged rm = new RijndaelManaged();
            rm.IV = rgbIV;
            rm.Key = keyBytes;

            ICryptoTransform cryp = rm.CreateDecryptor();
            byte[] outputBytes = cryp.TransformFinalBlock(inputBytes, 0, inputBytes.Length);
            return Encoding.UTF8.GetString(outputBytes);
        }

        /// <summary>
        ///  对已使用固定向量进行128位 AES加密的Base64字符串进行解密
        /// </summary>
        /// <param name="str">要解密的字符串</param>
        /// <param name="key">进行解密的128位密钥</param>
        /// <returns></returns>
        public static string AESDecrypt(this string str, string key)
        {
            return AESDecrypt(str, _defaultRgbIV128, key);
        }
        #endregion

        #endregion

        #region 类型转换

        /// <summary>
        /// 返回将字符串转换为它等效的 32 位有符号整数。如果转换失败则返回默认值
        /// </summary>
        /// <param name="str">包含要转换的数字的字符串</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static int ToInt(this string str, int defaultValue)
        {
            int value;
            if (Int32.TryParse(str, out value))
                return value;
            return defaultValue;

        }

        /// <summary>
        /// 返回将字符串转换为它等效的 64 位有符号整数。如果转换失败则返回默认值
        /// </summary>
        /// <param name="str">包含要转换的数字的字符串</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static long ToLong(this string str, long defaultValue)
        {
            long value;
            if (Int64.TryParse(str, out value))
                return value;
            return defaultValue;
        }

        /// <summary>
        ///  返回将字符串转换为它等效的 16 位有符号整数。如果转换失败则返回默认值
        /// </summary>
        /// <param name="str">包含要转换的数字的字符串</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static short ToShort(this string str, short defaultValue)
        {
            short value;
            if (Int16.TryParse(str, out value))
                return value;
            return defaultValue;
        }

        /// <summary>
        /// 返回将字符串转换为它等效的 System.Byte。如果转换失败则返回默认值
        /// </summary>
        /// <param name="str">包含要转换的数字的字符串</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static byte ToByte(this string str, byte defaultValue)
        {
            byte value;
            if (Byte.TryParse(str, out value))
                return value;
            return defaultValue;
        }

        /// <summary>
        /// 返回将字符串转换为它等效的单精度浮点数字。如果转换失败则返回默认值
        /// </summary>
        /// <param name="str">包含要转换的数字的字符串</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static float ToFloat(this string str, float defaultValue)
        {
            Single value;
            if (Single.TryParse(str, out value))
                return value;
            return defaultValue;
        }

        /// <summary>
        /// 返回将字符串转换为它等效的双精度浮点数字。如果转换失败则返回默认值
        /// </summary>
        /// <param name="str">包含要转换的数字的字符串</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static double ToDouble(this string str, double defaultValue)
        {
            Double value;
            if (Double.TryParse(str, out value))
                return value;
            return defaultValue;
        }

        /// <summary>
        /// 返回将字符串转换为它等效的System.Boolean 值。如果转换失败则返回默认值
        /// </summary>
        /// <param name="str">包含要转换的值的字符串</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static bool ToBoolean(this string str, bool defaultValue)
        {
            Boolean value;
            if (Boolean.TryParse(str, out value))
                return value;
            return defaultValue;
        }

        /// <summary>
        /// 返回将字符串转换为它等效的Unicode 字符。如果转换失败则返回默认值
        /// </summary>
        /// <param name="str">包含单个字符或 null 的字符串</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static char ToChar(this string str, char defaultValue)
        {
            Char value;
            if (Char.TryParse(str, out value))
                return value;
            return defaultValue;
        }

        /// <summary>
        ///  返回将字符串转换为它等效的 System.Decimal 表示形式。如果转换失败则返回默认值
        /// </summary>
        /// <param name="str">要转换的数字的字符串表示形式</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static decimal ToDecimal(this string str, Decimal defaultValue)
        {
            Decimal value;
            if (Decimal.TryParse(str, out value))
                return value;
            return defaultValue;
        }

        /// <summary>
        /// 返回将日期和时间的指定字符串表示形式转换为其 System.DateTime 等效项。如果转换失败则返回默认值
        /// </summary>
        /// <param name="str">包含要转换的日期和时间的字符串</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string str, DateTime defaultValue)
        {
            DateTime value;
            if (DateTime.TryParse(str, out value))
                return value;
            return defaultValue;
        }

        /// <summary>
        /// 返回将字符串转换为它等效的 32 位无符号整数。如果转换失败则返回默认值
        /// </summary>
        /// <param name="str">包含要转换的数字的字符串</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static uint ToUInt(this string str, uint defaultValue)
        {
            UInt32 value;
            if (UInt32.TryParse(str, out value))
                return value;
            return defaultValue;
        }

        /// <summary>
        /// 返回将字符串转换为它等效的 8 位有符号整数。如果转换失败则返回默认值
        /// </summary>
        /// <param name="str">包含要转换的数字的字符串</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static sbyte ToSByte(this string str, sbyte defaultValue)
        {
            SByte value;
            if (SByte.TryParse(str, out value))
                return value;
            return defaultValue;
        }

        /// <summary>
        /// 返回将字符串转换为它等效的 64 位无符号整数。如果转换失败则返回默认值
        /// </summary>
        /// <param name="str">包含要转换的数字的字符串</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static ulong ToULong(this string str, ulong defaultValue)
        {
            UInt64 value;
            if (UInt64.TryParse(str, out value))
                return value;
            return defaultValue;
        }


        /// <summary>
        /// 将字符串转换为指定的基元值类型/DateTime或者带有TypeConverter支持的值类型 如果转换失败则返回默认值
        /// </summary>
        /// <typeparam name="T">基元值类型/DateTime或者带有TypeConverter支持的值类型</typeparam>
        /// <param name="str">包含要转换的字符串</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static T ToType<T>(this string str, T defaultValue) where T : struct
        {
            T? value = str.ToType((T?)null);
            if (value.HasValue)
                return value.Value;

            return defaultValue;

        }

        /// <summary>
        /// 将字符串转换为指定的基元值类型/DateTime或者带有TypeConverter支持的值类型 如果转换失败则返回默认值或者null 
        /// </summary>
        /// <typeparam name="T">基元值类型/DateTime或者带有TypeConverter支持的值类型</typeparam>
        /// <param name="str">包含要转换的字符串</param>
        /// <param name="defaultValue">转换失败时返回的默认值或者null</param>
        /// <returns></returns>
        public static T? ToType<T>(this string str, T? defaultValue) where T : struct
        {
            if (string.IsNullOrEmpty(str))
                return defaultValue;

            Type type = typeof(T);
            ValueType value = null;
            #region 转换
            if (type == typeof(Int32))
            {
                Int32 v;
                if (Int32.TryParse(str, out v))
                {
                    value = v;
                }
            }
            else if (type == typeof(Int64))
            {
                Int64 v;
                if (Int64.TryParse(str, out v))
                {
                    value = v;
                }
            }
            else if (type == typeof(Boolean))
            {
                Boolean v;
                if (Boolean.TryParse(str, out v))
                {
                    value = v;
                }
            }
            else if (type == typeof(Int16))
            {
                Int16 v;
                if (Int16.TryParse(str, out v))
                {
                    value = v;
                }
            }
            else if (type == typeof(Byte))
            {
                Byte v;
                if (Byte.TryParse(str, out v))
                {
                    value = v;
                }
            }
            else if (type == typeof(Char))
            {
                Char v;
                if (Char.TryParse(str, out v))
                {
                    value = v;
                }
            }
            else if (type == typeof(Single))
            {
                Single v;
                if (Single.TryParse(str, out v))
                {
                    value = v;
                }
            }
            else if (type == typeof(Double))
            {
                Double v;
                if (Double.TryParse(str, out v))
                {
                    value = v;
                }
            }
            else if (type == typeof(Decimal))
            {
                Decimal v;
                if (Decimal.TryParse(str, out v))
                {
                    value = v;
                }
            }
            else if (type == typeof(DateTime))
            {
                DateTime v;
                if (DateTime.TryParse(str, out v))
                {
                    value = v;
                }
            }
            else if (type == typeof(SByte))
            {
                SByte v;
                if (SByte.TryParse(str, out v))
                {
                    value = v;
                }
            }
            else if (type == typeof(UInt16))
            {
                UInt16 v;
                if (UInt16.TryParse(str, out v))
                {
                    value = v;
                }
            }
            else if (type == typeof(UInt32))
            {
                UInt32 v;
                if (UInt32.TryParse(str, out v))
                {
                    value = v;
                }
            }
            else if (type == typeof(UInt64))
            {
                UInt64 v;
                if (UInt64.TryParse(str, out v))
                {
                    value = v;
                }
            }
            else
            {
                var converter = TypeDescriptor.GetConverter(type);
                if (converter != null && converter.CanConvertFrom(typeof(string)))
                {
                    try
                    {
                        value = (T)converter.ConvertFromString(str);

                    }
                    catch (NotSupportedException)
                    {
                        value = null;
                    }
                }

            }
            #endregion
            return value == null ? defaultValue : (T)value;
        }



        #endregion





    }

}
